#include "loginlogapi.h"

LoginLogApi::LoginLogApi(QObject *parent)
    : QObject{parent}
{
    baseUrl = Config::baseUrl;
    manager = new QNetworkAccessManager(0);
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));//post请求设置为json，才能发送josn格式文件
    request.setRawHeader("Authorization",  qApp->property("token").value<QString>().toLocal8Bit());
}

QByteArray *LoginLogApi::list(int pageNum, int pageSize, QString ipaddr, QString userName, QString status, QString start, QString end)
{
    QString params = "monitor/logininfor/list?pageNum="+ QString::number(pageNum) +
                     "&pageSize=" + QString::number(pageSize);
    if(!ipaddr.isEmpty()){
        params += "&ipaddr=" + ipaddr;
    }
    if(!userName.isEmpty()){
        params += "&userName=" + userName;
    }
    if(!status.isEmpty()){
        params += "&status=" + status;
    }
    if(start != "2000/1/1" && end != "2000/1/1"){
        params += "&params%5BbeginTime%5D=" + start + "&params%5BendTime%5D=" + end;
    }
    request.setUrl(baseUrl + params);
    QNetworkReply *reply= manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *LoginLogApi::clear()
{
    request.setUrl(baseUrl + "monitor/logininfor/clean");
    QNetworkReply *reply = manager->deleteResource(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *LoginLogApi::getById(int deptId)
{
    request.setUrl(baseUrl + "system/dept/" + QString::number(deptId));
    QNetworkReply *reply = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *LoginLogApi::deleteItems(QString ids)
{
    request.setUrl(baseUrl + "monitor/logininfor/" + ids);
    QNetworkReply * r = manager->deleteResource(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *LoginLogApi::unlock(QString userName)
{
    request.setUrl(baseUrl + "monitor/logininfor/unlock/" + userName);
    QNetworkReply * r = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}
